package edu.nf.bs.controller;

import com.github.pagehelper.PageInfo;
import edu.nf.bs.dao.OrderInfoMapper;
import edu.nf.bs.dao.TransferMapper;
import edu.nf.bs.dao.impl.LogisticsMapperImpl;
import edu.nf.bs.dao.impl.OrderInfoMapperImpl;
import edu.nf.bs.dao.impl.TransferMapperImpl;
import edu.nf.bs.entity.OrderInfo;
import edu.nf.bs.entity.Transfer;
import edu.nf.bs.service.impl.LogisticsServiceImpl;
import edu.nf.bs.service.impl.OrderInfoServiceImpl;
import edu.nf.bs.service.impl.TransferServiceImpl;
import edu.nf.bs.util.BaiDuMapUnit;
import edu.nf.bs.vo.AddressVO;
import edu.nf.bs.vo.ResultVO;
import edu.nf.bs.vo.UserAddressVO;
import org.mvc.web.annotation.RequestMapping;
import org.mvc.web.servlet.View;
import org.mvc.web.view.JsonView;

import java.util.List;


public class TransferContraller extends BaseContraller{
    @RequestMapping("/transfer/query")
    public View queryLogisticsId(String logisticsId){
        System.out.println("........................................");
        TransferMapper mapper=new TransferMapperImpl();
        TransferServiceImpl service=new TransferServiceImpl();
        service.setMapper(mapper);
        PageInfo<Transfer> transferPageInfo = service.pageTransfer(logisticsId);
        ResultVO<PageInfo<Transfer>> success = success(transferPageInfo);
        JsonView jsonView = new JsonView(success);
        jsonView.setDateFormat("yyyy-MM-dd hh:mm:ss");
        return jsonView;
    }
    @RequestMapping("/transfer/queryId")
    public View queryLoistics(String expressId){
        OrderInfoMapper orderInfoMapper=new OrderInfoMapperImpl();
        OrderInfoServiceImpl orderInfoService = new OrderInfoServiceImpl();
        orderInfoService.setOrderInfoMapper(orderInfoMapper);
        String s = orderInfoService.queryLogistics(expressId);
        TransferMapper mapper=new TransferMapperImpl();
        TransferServiceImpl service=new TransferServiceImpl();
        service.setMapper(mapper);
        PageInfo<Transfer> transferPageInfo = service.pageTransfer(s);
        ResultVO<PageInfo<Transfer>> success = success(transferPageInfo);
        return new JsonView(success);
    }
    @RequestMapping("/transfer/loisticsId")
    public View queryTransferId(String expressId){
        OrderInfoMapper orderInfoMapper=new OrderInfoMapperImpl();
        OrderInfoServiceImpl orderInfoService = new OrderInfoServiceImpl();
        orderInfoService.setOrderInfoMapper(orderInfoMapper);
        String loisticsId = orderInfoService.queryLogistics(expressId);
        TransferMapper mapper=new TransferMapperImpl();
        TransferServiceImpl service=new TransferServiceImpl();
        service.setMapper(mapper);
        PageInfo<Transfer> transferPageInfo = service.pageTransfer(loisticsId);
        ResultVO<PageInfo<Transfer>> success = success(transferPageInfo);
        JsonView jsonView = new JsonView(success);
        jsonView.setDateFormat("yyyy-MM-dd hh:mm:ss");
        return new JsonView(success);
    }
    @RequestMapping("/transfer/updateTransferStatus")
    public View updateTransferStatus(String logisticsId){
        TransferMapper mapper=new TransferMapperImpl();
        TransferServiceImpl service=new TransferServiceImpl();
        service.setMapper(mapper);
        String s = service.queryTransferId(logisticsId);
        service.updateTransfer(s);
        JsonView jsonView = new JsonView(success());
        jsonView.setDateFormat("yyyy-MM-dd hh:mm:ss");
        return jsonView;
    }
    @RequestMapping("/transfer/queryTransferStatus")
    public View queryTransferStatus(String logisticsId){
        TransferMapper mapper=new TransferMapperImpl();
        TransferServiceImpl service=new TransferServiceImpl();
        service.setMapper(mapper);
        List<Integer> integers = service.queryTransferStatus(logisticsId);
        Integer sum=0;
        for (Integer integer : integers) {
           sum+=integer;
        }
        if(sum==10){
            LogisticsMapperImpl logisticsMapper = new LogisticsMapperImpl();
            LogisticsServiceImpl logisticsService = new LogisticsServiceImpl();
            logisticsService.setLogisticsMapper(logisticsMapper);
            logisticsService.updateLogisticsStatus(logisticsId);

            OrderInfoMapperImpl orderInfoMapper = new OrderInfoMapperImpl();
            OrderInfoServiceImpl orderInfoService = new OrderInfoServiceImpl();
            orderInfoService.setOrderInfoMapper(orderInfoMapper);
            orderInfoService.updateOrderStatus(logisticsId);
        }
        ResultVO success = success();
        return new JsonView(success);
    }

    @RequestMapping("/transfer/queryTransferAddress")
    public View queryTransferAddress(String logisticsId){
        TransferMapperImpl transferMapper = new TransferMapperImpl();
        TransferServiceImpl transferService = new TransferServiceImpl();
        transferService.setMapper(transferMapper);
        Transfer transfer = transferService.queryTransferAddress(logisticsId);
        Transfer transfer1 = transferService.queryTransferAddress1(logisticsId);
        UserAddressVO geocoderLatitude = BaiDuMapUnit.getGeocoderLatitude(transfer.getTransferInfo());
        UserAddressVO geocoderLatitude1 = BaiDuMapUnit.getGeocoderLatitude(transfer1.getTransferInfo());
        AddressVO vo = new AddressVO(geocoderLatitude, geocoderLatitude1);
        ResultVO success = success(vo);
        return new JsonView(success);
    }
}
